<!--
  visible: 用于控制对话框显示隐藏
  title: 对话框标题
  width: 指定对话框的宽度
  footer插槽：用于指定对话框底部内容
  -->
<template>
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    width="30%"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入部门名称(1-50个字符)" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入部门编码(1-50个字符)" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          placeholder="请选择部门负责人"
          @focus="focusFn"
        >
          <el-option
            v-for="item in employeeList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          placeholder="部门介绍(必填1-300个字符)"
          type="textarea"
          rows="3"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="addFn">确 定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import {
  addDepartment,
  getDepartmentDetail,
  getDepartmentList
} from '@/api/departments'
import { getEmployeeSimple } from '@/api/employess'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      // 思路：
      // 1. 发送请求，获取到最新所有的部门信息
      const { depts } = await getDepartmentList()
      let result
      if (this.form.id) {
        if (value === this.nodeData.name) return callback()
        result = depts.filter(item => item.pid === this.nodeData.pid)
      } else {
        result = depts.filter(item => item.pid === this.nodeData.id)
      }
      // console.log(depts)
      // 2. 根据nodeData找到它的所有的子部门
      // console.log(result)
      // 3. 判断value和这些子部门的名称是否是重复的
      const has = result.some(item => item.name === value)
      has ? callback(new Error(`${value}已经存在了`)) : callback()
      // console.log(has)
    }
    const checkCode = async(rule, value, callback) => {
      if (this.form.id) {
        // 这是修改功能,允许编码和原来的一致
        if (value === this.nodeData.code) {
          return callback
        }
      }
      // 1. 发送请求-获取到最新的部门信息
      const { depts } = await getDepartmentList()
      // 2. 判断value是否和其他部门的code重复
      const has = depts.some(item => item.code === value)
      has ? callback(new Error(`${value}已经存在了`)) : callback()
      // console.log(has)
    }
    return {
      form: {
        name: '',
        manage: '',
        manager: '',
        introduce: '',
        code: ''
      },
      // 提供校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空' },
          { max: 50, message: '长度在1-50个字符' },
          // 自定义校验
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门名称不能为空' },
          { max: 50, message: '长度在1-50个字符' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空' },
          { max: 300, message: '部门介绍长度在1-300个字符' }
        ]
      },
      // 存放员工数据
      employeeList: []
      // username: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      // 重置表单的校验结果
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async focusFn() {
      // console.log('获取焦点')
      const data = await getEmployeeSimple()
      // console.log(data)
      this.employeeList = data
    },
    async addFn() {
      // console.log('-----')
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      // 发送请求
      // console.log('发送请求')
      await addDepartment({
        ...this.form,
        pid: this.nodeData.id
      })
      // console.log(res)
      // 提示
      this.$message.success('添加成功')
      // 关闭对话框
      this.$emit('update:showDialog', false)
      // 让父组件重新渲染
      this.$emit('getDepartments')
    },
    async getInfo(id) {
      console.log(id)
      const data = await getDepartmentDetail(id)
      this.form = data
    }
  }
}
</script>

<style>
.el-col {
  height: 30px;
  line-height: 30px;
}
.el-icon-caret-right:before {
  content: "\e723";
  color: #000;
}
.el-tree-node__expand-icon.expanded {
  transform: rotate(0deg);
}
.el-tree-node__expand-icon.expanded::before {
  content: "\e722";
  color: #000;
}

.el-tree-node__expand-icon.is-leaf::before {
  content: "\e7a5";
  color: #000;
}

</style>
